package com.test.htmlParser;

import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.NodeClassFilter;
import org.htmlparser.filters.OrFilter;
import org.htmlparser.tags.ImageTag;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import org.htmlparser.util.SimpleNodeIterator;

import com.lowagie.text.Image;



public class SimpleParser {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//extractKeyWordText("http://www.open-open.com/home/space.php?uid=34094&do=blog&id=5491","HttpClient");
//		extracLinks("http://www.12345.com");
		String frame = "<frame src=\"test.html\">";
		int start = frame.indexOf("src=");
		frame = frame.substring(start);
		int end = frame.indexOf(" ");
		if (end == -1)
			end = frame.indexOf(">");
		frame = frame.substring(5, end - 1);
		System.out.println(frame);
	}
	/**
	 * 从某个网页url中，找出某个关键字
	 */
	@SuppressWarnings("unused")
	private static void extractKeyWordText(String url, String keyword){
		try {
			// 生成一个解析器对象，用网页url做参数
			Parser parser = new Parser(url);
			parser.setEncoding("gb2312");
			// 迭代所有的节点 null表示不实用NodeFilter
			NodeList list = parser.parse(null);
			//从初始的节点列表跌倒所有的节点
			processNodeList(list, keyword);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 迭代节点
	 */
	private static void processNodeList(NodeList list, String keyword) {
		SimpleNodeIterator iterator = list.elements();
		while (iterator.hasMoreNodes()) {
			Node node = iterator.nextNode();
			// 得到该节点的子节点列表
			NodeList childList = node.getChildren();
			// 孩子为空，说明是值节点
			if (null == childList) {
				// 得到值节点的值
				String result = node.toPlainTextString();
				// 包含关键字打印出来
				if (result.indexOf(keyword) != -1) {
					System.out.println(result);
				}
			} else {
				processNodeList(childList, keyword);
			}
		}
	}
	
	
	/**
	 * 
	 *  获取一个网页上所有的链接和图片链接<a>,<img>,<frame>
	 */
	private static void extracLinks(String url){
		try {
			Parser parser = new Parser(url);
			parser.setEncoding("gb2312");
			//过滤 <frame> 标签的 filter，用来提取 frame 标签里的 src 属性所、表示的链接
			NodeFilter frameFilter = new NodeFilter(){
				@Override
				public boolean accept(Node paramNode) {
					if (paramNode.getText().startsWith("frame src=")) {
						return true;
					}
					return false;
				}
			};
			//OrFilter 来设置过滤 <a> 标签，<img> 标签和 <frame> 标签，三个标签是 or 的关系
			OrFilter orFilter = new OrFilter(new NodeClassFilter(LinkTag.class),new NodeClassFilter(Image.class));
			OrFilter linkFilter = new OrFilter(frameFilter,orFilter);
			//得到所有经过过滤的标签
			NodeList list = parser.extractAllNodesThatMatch(linkFilter);
			// 得到所有经过过滤的标签
			for (int i = 0; i < list.size(); i++) {
				Node tag = list.elementAt(i);
				if (tag instanceof LinkTag) { // <a> 标签
					LinkTag link = (LinkTag)tag;
					String linkUrl = link.getLink(); // url
					String text = link.getLinkText();// /链接文字
					System.out.println(linkUrl + "      " + text);
				} else if (tag instanceof ImageTag) {//<image>标签
					ImageTag image = (ImageTag) tag;
					String iamgeUrl = image.getImageURL();
					String text = image.getText();
					System.out.println(iamgeUrl+"     "+text);
				} else {// <frame>标签
					//提取 frame 里 src 属性的链接如 <frame src="test.html"/>
					String frame = tag.getText();
					int start = frame.indexOf("src=");
					frame = frame.substring(start);
					int end = frame.indexOf(" ");
					if (end == -1)
						end = frame.indexOf(">");
					frame = frame.substring(5, end - 1);
					System.out.println(frame+"ddddd");
				}
			}
		} catch (ParserException e) {
			e.printStackTrace();
		}
	}

}
